package com.longcai.cm.controller;

import com.longcai.cm.base.aop.Log;
import com.longcai.cm.base.bean.PageVo;
import com.longcai.cm.base.bean.Pageable;
import com.longcai.cm.bean.R;
import com.longcai.cm.common.utils.RequestUtil;
import com.longcai.cm.common.utils.StringUtil;
import com.longcai.cm.service.JaBannerService;
import com.longcai.cm.domain.JaBanner;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author LBX
 * @date 2024-08-09
 */
@RestController
@RequestMapping("/JaBanner")
public class JaBannerController {

    private final JaBannerService jaBannerService;

    public JaBannerController(JaBannerService jaBannerService) {
        this.jaBannerService = jaBannerService;
    }

    @RequestMapping("/getList")
    public R<PageVo<JaBanner>> getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<JaBanner> result = jaBannerService.getPageBean(pageInfo, JaBanner.class);
        return R.success(result);
    }

    /**
     * @param vo 插入实体
     * @return R
     */
    @Log
    @RequestMapping("/save")
    public R<String> save(JaBanner vo) {
        if (StringUtils.isNotBlank(vo.getId())) {
            boolean success = jaBannerService.updateById(vo);
            return success ? R.success() : R.error();
        } else {
            final String uuid = StringUtil.getUuid();
            vo.setId(uuid);
            boolean success = jaBannerService.save(vo);
            return success ? R.success() : R.error();
        }
    }

    /**
     * @param vo      修改实体
     * @param request id
     * @return R
     */
    @Log
    @RequestMapping("/edit")
    public R<String> edit(JaBanner vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            boolean success = jaBannerService.updateById(vo);
            return success ? R.success() : R.error();
        } else {
            return R.error("id不能为空");
        }
    }

    /**
     * @param id 主键
     * @return R
     */
    @Log
    @RequestMapping("/delete/{id}")
    public R<String> deleteByPrimaryKey(@PathVariable("id") Integer id) {
        final boolean success = jaBannerService.removeById(id);
        return success ? R.success() : R.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return R
     */
    @Log
    @RequestMapping("/delete")
    public R<String> delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            final boolean success = jaBannerService.removeById(idString);
            if (!success) {
                return R.error();
            }
        }
        return R.success();
    }
}